package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsAllPowerfulMachineBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsAllPowerfulMachineVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsAllPowerfulMachine;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsAllPowerfulMachineMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsAllPowerfulMachineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 万能机TestMaster软件上传数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsAllPowerfulMachineServiceImpl implements INLaboratoryMechanicsAllPowerfulMachineService {

    private final NLaboratoryMechanicsAllPowerfulMachineMapper baseMapper;

    /**
     * 查询万能机TestMaster软件上传数据
     */
    @Override
    public NLaboratoryMechanicsAllPowerfulMachineVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机TestMaster软件上传数据列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsAllPowerfulMachineVo> queryPageList(NLaboratoryMechanicsAllPowerfulMachineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachine> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsAllPowerfulMachineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机TestMaster软件上传数据列表
     */
    @Override
    public List<NLaboratoryMechanicsAllPowerfulMachineVo> queryList(NLaboratoryMechanicsAllPowerfulMachineBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachine> buildQueryWrapper(NLaboratoryMechanicsAllPowerfulMachineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachine> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSampleKey()), NLaboratoryMechanicsAllPowerfulMachine::getSampleKey, bo.getSampleKey());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryMechanicsAllPowerfulMachine::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getTestId()), NLaboratoryMechanicsAllPowerfulMachine::getTestId, bo.getTestId());
        lqw.eq(StringUtils.isNotBlank(bo.getTester()), NLaboratoryMechanicsAllPowerfulMachine::getTester, bo.getTester());
        lqw.eq(StringUtils.isNotBlank(bo.getGczj()), NLaboratoryMechanicsAllPowerfulMachine::getGczj, bo.getGczj());
        lqw.eq(StringUtils.isNotBlank(bo.getArea()), NLaboratoryMechanicsAllPowerfulMachine::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getSo()), NLaboratoryMechanicsAllPowerfulMachine::getSo, bo.getSo());
        lqw.eq(StringUtils.isNotBlank(bo.getShape()), NLaboratoryMechanicsAllPowerfulMachine::getShape, bo.getShape());
        lqw.eq(bo.getCreateDate() != null, NLaboratoryMechanicsAllPowerfulMachine::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCreator()), NLaboratoryMechanicsAllPowerfulMachine::getCreator, bo.getCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getSize()), NLaboratoryMechanicsAllPowerfulMachine::getSize, bo.getSize());
        lqw.eq(StringUtils.isNotBlank(bo.getSpan()), NLaboratoryMechanicsAllPowerfulMachine::getSpan, bo.getSpan());
        lqw.eq(StringUtils.isNotBlank(bo.getLength()), NLaboratoryMechanicsAllPowerfulMachine::getLength, bo.getLength());
        lqw.eq(StringUtils.isNotBlank(bo.getWidth()), NLaboratoryMechanicsAllPowerfulMachine::getWidth, bo.getWidth());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), NLaboratoryMechanicsAllPowerfulMachine::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getDoo()), NLaboratoryMechanicsAllPowerfulMachine::getDoo, bo.getDoo());
        lqw.eq(StringUtils.isNotBlank(bo.getZj()), NLaboratoryMechanicsAllPowerfulMachine::getZj, bo.getZj());
        return lqw;
    }

    /**
     * 新增万能机TestMaster软件上传数据
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsAllPowerfulMachineBo bo) {
        NLaboratoryMechanicsAllPowerfulMachine add = BeanUtil.toBean(bo, NLaboratoryMechanicsAllPowerfulMachine.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改万能机TestMaster软件上传数据
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsAllPowerfulMachineBo bo) {
        NLaboratoryMechanicsAllPowerfulMachine update = BeanUtil.toBean(bo, NLaboratoryMechanicsAllPowerfulMachine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsAllPowerfulMachine entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机TestMaster软件上传数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 万能机设备列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsAllPowerfulMachineVo> page(NLaboratoryMechanicsAllPowerfulMachineBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachine> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsAllPowerfulMachineVo> result = baseMapper.page(pageQuery.build(), bo);
        // 获取主表ids
        List<NLaboratoryMechanicsAllPowerfulMachineVo> records = result.getRecords();
        List<String> ids = records.stream().map(NLaboratoryMechanicsAllPowerfulMachineVo::getId).collect(Collectors.toList());
        // 获取试样数量 原始标距
        if (CollectionUtils.isNotEmpty(ids)) {
            List<NLaboratoryMechanicsAllPowerfulMachineVo> details = baseMapper.getDetailInfo(ids);
            // 转map合并对象信息
            Map<String, NLaboratoryMechanicsAllPowerfulMachineVo> map =
                details.stream().collect(Collectors.toMap(NLaboratoryMechanicsAllPowerfulMachineVo::getId, a -> a));
            for (NLaboratoryMechanicsAllPowerfulMachineVo record : records) {
                if (map.containsKey(record.getId())) {
                    record.setSampleNum(map.get(record.getId()).getSampleNum());
                    record.setLo(map.get(record.getId()).getLo());
                }
            }
            result.setRecords(records);
        }
        return TableDataInfo.build(result);
    }
}
